pandas Series

pandas包提供了两种数据类型,即Series和DataFrame,分别对应于一维数据和二维数据。跟NumPy数组不同的是,Series和DataFrame是带索引的一维数据和二维数据。

下面用pandas包的Series方法创建一个Series类型的对象并用变量ser引用它:

code.python
>>> import pandas as pd
>>> ser=pd.Series([10,20,30,40])

查看ser:

code.python
>>> ser
0    10
1    20
2    30
3    40
dtype: int64

可见,Series类型的数据显示为两列,第1列为索引标签,第2列为数据一维数组。如果把索引看作是key,那么它是一个类似字典的数据结构,每一条数据由索引标签和对应的值组成。

创建Series类型的对象

上面使用pandas包的Series方法创建了一个Series类型的对象。它实际上是利用列表数据创建的。使用Series方法,还可以将元组数据、字典数据、NumPy数组等转换为Series类型的对象。

下面通过元组数据创建Series类型的对象。

code.python
>>> ser=pd.Series((10,20,30,40))
>>> ser
0    10
1    20
2    30
3    40
dtype: int64

通过字典数据创建Series类型的对象。此时字典数据的键被转换为Series数据的索引。

code.python
>>> ser=pd.Series({'a':10, 'b':20, 'c':30, 'd':40})
>>> ser
a    10
b    20
c    30
d    40
dtype: int64

通过NumPy数组创建Series类型的对象。

code.python
>>> ser=pd.Series(np.arange(10,50,10))
>>> ser
0    10
1    20
2    30
3    40
dtype: int32

上面创建Series对象时,除利用字典创建时外,Series数据的索引都是自动创建的,为0基数的顺序递增的整数。实际上,创建Series对象时,可以使用index参数指定索引。下面用index参数指定所创建的Series数据的索引。

code.python
>>> ser=pd.Series(np.arange(10,50,10),index=['a','b','c','d'])
>>> ser
a    10
b    20
c    30
d    40
dtype: int32

还可以使用name参数指定Series对象的名称。

code.python
>>> ser=pd.Series(np.arange(10,50,10),index=['a','b','c','d'],name='得分')
>>> ser
a    10
b    20
c    30
d    40
Name: 得分, dtype: int32

Series对象的描述

使用Series对象的shape, size, index, values等属性可以获取数据形状、大小、索引标签和值等数据。下面创建一个Series类型的对象ser:

code.python
>>> ser=pd.Series(np.arange(10,50,10),index=['a','b','c','d'])
>>> ser
a    10
b    20
c    30
d    40
dtype: int32

用shape属性获取ser的形状。

code.python
>>> ser.shape
(4,)

用size属性获取ser的大小。

code.python
>>> ser.size
4

用index属性获取ser的索引标签。

code.python
>>> ser.index
Index(['a','b','c','d'], dtype='object')

用values属性获取ser的值。

code.python
>>> ser.values
array([10, 20, 30, 40])

使用Series对象的head和tail方法可以获取对象中前面和后面指定个数的数据。默认时个数为5。下面获取ser中前两个和后两个数据。

code.python
>>> ser.head(2)
a    10
b    20
dtype: int32
>>> ser.tail(2)
c    30
d    40
dtype: int32

索引和切片

创建Series类型的数据后,如果希望提取其中的某个值或某些值,需要通过索引或切片来实现。可以使用中括号获取单个索引,此时返回的是元素类型;或者中括号里面用一个列表获取多个索引,此时返回的是一个Series类型。

下面创建一个Series类型的数据ser:

code.python
>>> ser=pd.Series(np.arange(10,50,10),index=['a','b','c','d'])
>>> ser
a    10
b    20
c    30
d    40
dtype: int32

获取第2个值,它的索引标签为"b"。

code.python
>>> r1=ser['b']
>>> r1
20

用type函数获取r1的数据类型:

code.python
>>> type(r1)
<class 'numpy.int32'>

返回的是元素的数据类型。

下面获取第1个和第4个值,使用它们的索引标签组成的列表进行获取。

code.python
>>> r2=ser[['a', 'd']]
>>> r2
a    10
d    40
Name: 得分, dtype: int32

用type函数获取r2的数据类型:

code.python
>>> type(r2)
<class 'pandas.core.series.Series'>

可见,此时返回的是Series类型的数据。

除了使用中括号,还可以使用Series类型对象的loc方法和iloc方法进行索引。loc方法使用数据的索引标签进行索引,iloc方法则使用顺序编号进行索引。

下面获取ser中索引标签"a"和"d"对应的值。

code.python
>>> r3=ser.loc[['a','d']]
>>> r3
a    10
d    40
Name: 得分, dtype: int32

用iloc方法获取ser中的第1和第4条数据。

code.python
>>> r4=ser.iloc[[0,3]]
>>> r4
a    10
d    40
Name: 得分, dtype: int32

使用冒号,可以对Series数据进行切片。下面对ser数据中从"a"标签到"c"标签连续获取值。

code.python
>>> r5=ser['a':'c']
>>> r5
a    10
b    20
c    30
Name: 得分, dtype: int32

下面用iloc方法获取ser中第2个及以后的所有数据。

code.python
>>> r6=ser.iloc[1:]
>>> r6
b    20
c    30
d    40
Name: 得分, dtype: int32

布尔索引

在中括号中使用布尔表达式可以实现布尔索引。

下面获取ser中值不超过20的数据。

code.python
>>> ser[ser.values<=20]
a    10
b    20
dtype: int32

下面获取ser中索引标签不为"a"的数据。

code.python
>>> ser[ser.index!='a']
b    20
c    30
d    40
dtype: int32